package com.java.crazy.part1.流程控制;

import com.java.crazy.base.BaseClass;

import java.util.Arrays;

public class CollectionDemo extends BaseClass {
    public static String[] strings = {"s1", "s2", "s3", "s3"};

    public static void main(String[] args) {
        logi("流程控制：");
        forMain();
        switchMain();
        arrayMain();
        array8Main();
    }

    private static void forMain() {
        logi("\n流程控制：for  ");
        first:
        for (int i = 0; i < 3; i++) {
            two:
//循环的标签，可以使用break two，结束循环。
            for (int j = 0; j < 3; j++) {
                logi("j==" + j + "  ----  i=" + i);
                if (j == 2) {
                    logi("j==2关闭two循环：i=" + i);
                    break two;
                }
                if (i == 1) {
                    logi("i==1关闭first循环：i=" + i);
                    break first;
                }
            }
        }
    }

    private static void switchMain() {
        logi("\n流程控制：switch case");
        for (String str : strings) {
            switch (str) {
                case "s1":
                    logi("是s1----" + str);
                    break;
                case "s2":
                    logi("是s2-----" + str);
                    break;
                default:
                    logi("不是s1不是s2而是 str=" + str);
                    break;
            }
        }
    }

    private static void arrayMain() {
        logi("\n流程控制：数组");
        String[] ints;
        ints = new String[3];
        logi("初始化后的值：");//int形的是0，其他是null
        for (int i = 0; i < ints.length; i++) {
            logi(ints[i]);
        }
        logi("赋值中...");
        for (int i = 0; i < ints.length; i++) {
            ints[i] = i + 10 + "";
        }
        logi("赋值后的值：");
        for (int i = 0; i < ints.length; i++) {
            logi(ints[i]);
        }
    }

    private static void array8Main() {
        logi("\n流程控制：Arrays 查找值");
        int result = Arrays.binarySearch(strings, "s3");
        if (result >= 0) {
            logi("下标为：" + result);
        } else {
            logi("无此数据：" + result);//返回一个负数: -（strings.size()+1）
        }
    }
}
